package com.admin.controller.system;

import java.util.List;
import java.util.Map;

import com.admin.vo.system.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.admin.common.enums.EnumResult;
import com.admin.common.util.YsCollectionUtil;
import com.admin.common.util.YsStringUtil;
import com.admin.controller.base.BaseController;
import com.admin.domain.RoleDO;
import com.admin.req.system.SaveRoleReq;
import com.admin.req.system.UpdateRoleReq;
import com.admin.service.MenuService;
import com.admin.service.RoleService;
import com.admin.vo.PageVO;
import com.admin.vo.ResultVO;
import com.admin.vo.SimpleResultVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 角色管理
 * 
 * @author ZY
 * @date 2018/10/18
 */
@Api(description = "角色管理")
@RestController
@RequestMapping(value = "/role")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;

    @ApiOperation(value = "角色列表")
    @RequiresPermissions("role:list")
    @GetMapping(value = "/list")
    @ApiImplicitParams(
        value = {
                @ApiImplicitParam(name = "pageIndex", value = "当前页码", dataType = "int", paramType = "query"),
                @ApiImplicitParam(name = "pageSize", value = "每页数量", dataType = "int", paramType = "query"),
                @ApiImplicitParam(name = "roleName", value = "角色名称", dataType = "String", paramType = "query"),
                @ApiImplicitParam(name = "roleLevel", value = "角色等级", dataType = "String", paramType = "query"),
                @ApiImplicitParam(name = "responsibleAreaId", value = "区域", dataType = "String", paramType = "query")})
    public ResultVO<PageVO<ListRoleVO>> listRole(Integer pageIndex, Integer pageSize,
                                                 String roleName,String roleLevel, String responsibleAreaId) {
        Map<String, Object> params = YsCollectionUtil.createHashMap();
        params.put("roleName", roleName);
        params.put("roleLevel", roleLevel);
        params.put("responsibleAreaId", responsibleAreaId);
        params.put("orderBy", "role_id");
        return roleService.getPageList(pageIndex, pageSize, params);
    }


    @ApiOperation(value = "角色列表")
    @GetMapping(value = "/roles")
    @ApiImplicitParams(
            value = {
                    @ApiImplicitParam(name = "responsibleAreaId", value = "负责区域", dataType = "String", paramType = "query", required = true),
                    @ApiImplicitParam(name = "type", value = "0 申报用户 1 其它", dataType = "int", paramType = "query"),
                    @ApiImplicitParam(name = "userId", value = "用户id", dataType = "int", paramType = "query")})
    public ResultVO<List<RoleSelectedVO>> getSelectRolesByAreaId(String responsibleAreaId, Integer type, Integer userId) {
        return roleService.getSelectRolesByAreaId(responsibleAreaId, type, userId);
    }




    @ApiOperation(value = "到新增角色页")
    @RequiresPermissions("role:save")
    @GetMapping("/toAdd")
    public ResultVO<AddRoleVO> toAddRole() {
        ResultVO<AddRoleVO> result = new ResultVO<>();
        AddRoleVO addRoleVO = new AddRoleVO();
        addRoleVO.setMenuVOs(menuService.getSelectMenus().getData());

        result.setData(addRoleVO);
        result.setCode(EnumResult.SUCCESS.getCode());
        result.setMsg(EnumResult.SUCCESS.getMsg());
        return result;
    }



    @ApiOperation(value = "选择权限菜单")
    @GetMapping("/selectMenus")
    @ApiImplicitParams(
            value = {@ApiImplicitParam(name = "type", value = "province，city, area", dataType = "String", paramType = "query")})
    public ResultVO<List<MenuSelectedVO>> selectMenus(String type) {
        return ResultVO.ok(menuService.selectMenus(type));
    }


    @ApiOperation(value = "新增角色")
    @RequiresPermissions("role:save")
    @PostMapping(value = "/save")
    public SimpleResultVO saveRole(@RequestBody SaveRoleReq req) {
        RoleDO roleDO = new RoleDO();
        BeanUtils.copyProperties(req, roleDO);
        return roleService.saveRoleDO(roleDO, req.getMenuIds());
    }

    @ApiOperation(value = "到修改角色页")
    @RequiresPermissions("role:list")
    @GetMapping(value = "/toUpdate")
    @ApiImplicitParam(name = "roleId", value = "角色id", required = true, dataType = "long", paramType = "query")
    public ResultVO<UpdateRoleVO> toUpdateRole(Long roleId) {
        ResultVO<UpdateRoleVO> result = new ResultVO<UpdateRoleVO>();
        UpdateRoleVO updateRoleVO = new UpdateRoleVO();
        RoleDO roleDO = roleService.selectByPrimaryKey(roleId);
        BeanUtils.copyProperties(roleDO, updateRoleVO);
        updateRoleVO.setMenuVOs(menuService.getSelectMenus(roleDO.getRoleId()).getData());

        result.setData(updateRoleVO);
        result.setCode(EnumResult.SUCCESS.getCode());
        result.setMsg(EnumResult.SUCCESS.getMsg());
        return result;
    }

    @ApiOperation(value = "修改角色")
    @RequiresPermissions("role:update")
    @PostMapping(value = "/update")
    public SimpleResultVO updateRole(@RequestBody UpdateRoleReq req) {
        RoleDO roleDO = new RoleDO();
        BeanUtils.copyProperties(req, roleDO);
        return roleService.upateRoleDO(roleDO, req.getMenuIds());
    }

    @ApiOperation(value = "删除角色")
    @RequiresPermissions("role:delete")
    @GetMapping(value = "/delete")
    @ApiImplicitParam(name = "roleId", value = "角色id", required = true, dataType = "long", paramType = "query")
    public SimpleResultVO deleteRole(Long roleId) {
        SimpleResultVO result = new SimpleResultVO();

        roleService.deleteByPrimaryKey(roleId);

        result.setCode(EnumResult.SUCCESS.getCode());
        result.setMsg(EnumResult.SUCCESS.getMsg());
        return result;
    }

    @ApiOperation(value = "批量删除角色")
    @RequiresPermissions("role:batchDelete")
    @PostMapping(value = "/batchDelete")
    @ApiImplicitParam(name = "roleIds", value = "一组角色id，英文逗号分隔", required = true, dataType = "String",
        paramType = "query")
    public SimpleResultVO deleteRoles(String roleIds) {
        return roleService.deleteByPrimaryKeys(YsStringUtil.splitToLong(roleIds, ","));
    }
}
